﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using insidedotnet.UI;

public partial class AdminCriarAnunciante : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = AcheiCampinasConfiguration.SiteName +
            " : criar anúncio de empresa.";

        // Fill the controls with data only on the initial page load
        if (!IsPostBack)
        {
            // Fill controls with data
            PopulateControls();
        }
    }

    // Populate the controls
    private void PopulateControls()
    {
        // Fill categoriesLabel and categoriesListRemove with data
        string categoryId, categoryName, bairroName, bairroId;
        DataTable productCategories = CatalogAccess.GetCategorias();

        for (int i = 0; i < productCategories.Rows.Count; i++)
        {
            // obtain category id and name
            categoryId = productCategories.Rows[i]["categoriaID"].ToString();
            categoryName = productCategories.Rows[i]["nome"].ToString();
            // populate the list boxes
            categoriesListAssign.Items.Add(new ListItem(categoryName, categoryId));
        }

        DataTable bairros = CatalogAccess.GetBairros();

        for (int i = 0; i < bairros.Rows.Count; i++)
        {
            // obtain category id and name
            bairroId = bairros.Rows[i]["bairroID"].ToString();
            bairroName = bairros.Rows[i]["nome"].ToString();
            // populate the list boxes
            bairroList.Items.Add(new ListItem(bairroName, bairroId));
        }
    }

    // Create a new product
    protected void criarAnunciante_Click(object sender, EventArgs e)
    {
        DateTime agora = DateTime.Now;
        string location;

        string fileName1 = agora.ToString("dd_MM_yyyy_HH_mm_ss_") + image1FileUpload.FileName;

        if (image1FileUpload.FileName != "")
        {
            location = Server.MapPath("./ImagensLogos/") + fileName1;
            // save image to server
            image1FileUpload.SaveAs(location);

            //create its thumbnail
            Thumbnail.Create(Server.MapPath(""), "ImagensLogos", "ImagensLogos", fileName1, 240, 230);

            //delete the original image
            File.Delete(location);

        }
        else
        {
            fileName1 = "sem_imagem.gif";
        }

        int n = int.Parse(bairroList.SelectedItem.Value);

         // Execute the insert command
        bool successo = CatalogAccess.CriarAnunciante(nomeEmpresa.Text, descricaoEmpresa.Text, endereco.Text,
            numero.Text, bairroList.SelectedItem.Text, cep.Text, telefone.Text, celular.Text, paginaweb.Text, email.Text, 
            msn.Text, twitter.Text, facebook.Text, orkut.Text, blog.Text, youtube.Text, "tb_"+fileName1, categoriesListAssign.SelectedItem.Value, bairroList.SelectedItem.Value);

        // Display status message
        statusLabel.Text = successo ? "Anunciante criado com sucesso." : "Erro na criação do anunciante.";
        // Redirect to AdminAnuncio.aspx
        Response.Redirect("Mensagens.aspx?n=1");
    }
}